package masterjava.common.client.gxt.base.tabs;

import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.TabItem;
import masterjava.common.shared.HasKey;

import java.util.HashMap;
import java.util.Map;

/**
 * User: gkislin
 * Date: 7/7/11
 */
abstract public class BaseNamedTabs<T extends HasKey, M extends Component> extends BaseTabs<T> {

    private final Map<T, M> tabMap = new HashMap<T, M>();

    public BaseNamedTabs(T tabItem) {
        super(tabItem);
    }

    public BaseNamedTabs(T[] tabItems) {
        super(tabItems);
    }

    public BaseNamedTabs(Iterable<T> tabIterable) {
        super(tabIterable);
    }

    @Override
    protected TabItem createTabItem(T item) {
        TabItem tabItem = new TabItem(item.getKey());
        M tabWidget = createTabComponent(item);
        tabMap.put(item, tabWidget);
        tabItem.add(tabWidget);
        add(tabItem);
        return tabItem;
    }

    protected abstract M createTabComponent(T item);

    public M getTabWidget(T key) {
        return tabMap.get(key);
    }

    public Map<T, M> getTabMap() {
        return tabMap;
    }
}
